package com.miracle9.game.service;

import com.miracle9.game.dao.UserAwardServiceDao;
import com.miracle9.game.dao.UserServiceDao;
import com.miracle9.game.bean.Pager;
import com.miracle9.game.entity.User;
import com.miracle9.game.entity.UserAward;
import com.miracle9.game.util.MyUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userAwardService")
public class UserAwardService
{
  @Autowired
  private UserAwardServiceDao userAwardServiceDao;
  @Autowired
  private UserServiceDao userServiceDao;
  
  public Map<String, Object> getUserAward(int userId, String startDate, String endDate, Pager pager, IoSession session)
  {
    Map<String, Object> map = new HashMap();
    double lastTotalGold = 0.0D;
    endDate = MyUtil.DateByDayCalendar(endDate, 1);
    if ((pager.getOrder() == "") && (pager.getOrderBy() == ""))
    {
      pager.setOrderBy("datetime");
      pager.setOrder("desc");
    }
    User user = (User)this.userServiceDao.getById(Integer.valueOf(userId));
    pager = this.userAwardServiceDao.q_CreateSqlsPager("from UserAward where userId=? and datetime>=? and datetime<=?", pager, new Object[] {
      Integer.valueOf(userId), startDate, endDate });
    List<Object> datas = new ArrayList();
    for (Object o : pager.getList())
    {
      UserAward ua = (UserAward)o;
      Map<String, Object> data = new HashMap();
      data.put("datetime", ua.getDatetime());
      data.put("username", user.getUsername());
      data.put("gold", Integer.valueOf(ua.getGold()));
      data.put("admin", ua.getAdmin());
      datas.add(data);
    }
    pager.setList(datas.toArray());
    map.put("pager", pager);
    String hql1 = "select ifnull(sum(gold),0) from user_award where user_id=" + userId + " and datetime>=? and datetime<=? ";
    
    List log = this.userServiceDao.returnList_3(hql1, startDate, endDate);
    for (Object o1 : (List) log) {
      lastTotalGold = Double.parseDouble(o1.toString());
    }
    map.put("lastTotalGold", Double.valueOf(lastTotalGold));
    return map;
  }
  
  public void deletePastAward(String date)
  {
    this.userAwardServiceDao.deletePastAward(date);
  }
}
